package uppgift1del2;

public class Uppgift2 {
	private static double temp, result;
	
	static public double binarySqrt(double sqr, double eps) {
		if(sqr >= 1 && eps > 0) {
			try {
				result = help(sqr, eps, 1, sqr);
			} catch (Error stackOverflowError) {
				System.out.println("StackOverFlow error.");
			}
		} else {
			throw new IllegalArgumentException("sqr has to be >= 1 and eps has to be positive.");
		}
		return result;
	}
	
	static private double help(double sqr, double eps, double low, double high) {
		temp = ((high-low)/2+low);
		if (temp*temp > sqr+eps) {
			help(sqr, eps, low, temp);
		} else if(temp*temp < sqr-eps) {
			help(sqr, eps, temp, high);
		}
		return temp;
	}
}
